(function() {
  console.log('fatie.js');

  var app = new Vue({
    el: '#app',
    data: {
      user: {},
      subType: { ttid: -1 },
      list: []
    },
    methods: {
      logout: logout,
      showTypeName: showTypeName,
      query: query,
      cancel: cancel,
      queryUserInfo: queryUserInfo,
      add:add
    }
  });

  function add() {
    huhuiyu.send(
      '/usersubtype/add',
      {
        subType: app.subType
      },
      function(data) {
        alert(data.message);
        if (data.success) {
          app.subType = { ttid: -1 };
        }
      }
    );
  }

  function showTypeName(ttid) {
    for (var i = 0; i < app.types.length; i++) {
      var type = app.types[i];
      if (ttid == type.ttid) {
        return type.typeName;
      }
    }
    return '';
  }

  function cancel() {
    app.inmodify = false;
    app.subType = { ttid: -1 };
  }

  function logout() {
    huhuiyu.send('/user/logout', {}, function(data) {
      alert(data.message);
      location = 'index.html';
    });
  }

  //获取登录用户信息
  function queryUserInfo() {
    huhuiyu.send('/user/getUserInfo', {}, function(data) {
      console.log(data);
      if (!data.success || !data.datas || !data.datas.user) {
        alert('需要登录！');
        location = 'index.html';
        return;
      }
      if (data) {
        app.user = data.datas.user;
        return;
      }
    });
  }

  //查询种类
  function query() {
    huhuiyu.send('/user/TypequeryAll', {}, function(data) {
      if (data.success) {
        app.list = data.datas.list;
        return;
      }
      alert(data.message);
    });
  }
  query();
  queryUserInfo();
})();
